home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / SpellCompositor / SpellCompositor.java < prev    next >
Encoding:
Text File  |  2001-06-23  |  26.0 KB  |  843 lines

  1. /*
  2.     SpellCompositor.java
  3.         - development pre-cursor to ArcanaApp, used for experimentation and debugging/testing
  4.     
  5.     Written by Scott C. Ziegler for use with Simulcra RPG and AD&D
  6.     
  7.     vers. history:
  8.         12.21.00 - added comment section, fixed navigation buttons, added book info accessors.
  9.     
  10.     Work Needed:
  11.         - need to implement navigation buttons, make sure they don't move unless current spell is saved!
  12.         - need to check for duplicates before adding a spell.
  13.         - fix up removal code with JDialog/JOptionPane.
  14.     Work Completed:
  15.         Date - Desc
  16.  
  17.  
  18. */
  19.  
  20. /*
  21. This file and its intellectual contents are considered property of the creator and are to be treated as such with respect to use in other applications.  Any use of this software for any purpose whatsoever must have explicit permission from the author of the file.  This code is part of an ongoing development process for future possible products, and so is considered private property.
  22. Do not use without permission.  Author: Scott C. Ziegler <Aslan@Narnia.net>
  23. */
  24.  
  25. //package Arcana;
  26.  
  27. // initEditor needs work (see comments)
  28.  
  29. import java.awt.*;
  30. import java.awt.event.*;
  31. import java.io.*;
  32. import java.util.*;
  33. import javax.swing.*;
  34. import javax.swing.event.*;
  35.  
  36.  
  37. public class SpellCompositor {
  38.     
  39.     static final String DEFAULT_SPELLFILE_PATH = "untitled.spellbook";
  40.     
  41.     InteractionFrame        hostFrame;
  42.     Thoth.SpellEditFrame    editFrame;
  43.     Thoth.SpellViewFrame    viewFrame;
  44.     Thoth.SpellBrowserFrame    browserFrame;
  45.     
  46.     Arcana.SpellFile    spellFile;
  47.     Arcana.SpellBook    spellBook;
  48.     
  49.     Arcana.SpellRecord    lastSpell;
  50.     Arcana.SpellRecord    currentSpell;
  51.     
  52.     int            currentLevel;
  53.     int            currentIndexInLevel;
  54.     boolean        fileTouched;
  55.     
  56.     FileReader         fr;
  57.     BufferedReader     in;
  58.     
  59.     char[]     buffer;
  60.     int     bufferIndex = 0;
  61.     int     bytesRead;
  62.     short     asciiChar;
  63.     
  64.     public SpellCompositor() {
  65.         // IMPL. Vars.
  66.         fileTouched = false;
  67.         currentLevel = 1;
  68.         currentIndexInLevel = 0;
  69.         // GUI
  70.         hostFrame = new InteractionFrame();
  71.         editFrame = new Thoth.SpellEditFrame();
  72.         initEditor();
  73.         viewFrame = new Thoth.SpellViewFrame();
  74.         initViewer();
  75.         browserFrame = new Thoth.SpellBrowserFrame();
  76.         initBrowser();
  77.         // MODEL
  78.         spellFile = new Arcana.SpellFile(DEFAULT_SPELLFILE_PATH);
  79.         spellBook = new Arcana.SpellBook();
  80.         lastSpell = new Arcana.SpellRecord();
  81.         currentSpell = new Arcana.SpellRecord();
  82.         }
  83.     
  84.     public SpellCompositor(InteractionFrame hFrame) {
  85.         // IMPL. Vars.
  86.         fileTouched = false;
  87.         currentLevel = 1;
  88.         currentIndexInLevel = 0;
  89.         // GUI
  90.         hostFrame = hFrame;
  91.         editFrame = new Thoth.SpellEditFrame();
  92.         initEditor();
  93.         viewFrame = new Thoth.SpellViewFrame();
  94.         initViewer();
  95.         browserFrame = new Thoth.SpellBrowserFrame();
  96.         initBrowser();
  97.         // MODEL
  98.         spellFile = new Arcana.SpellFile(DEFAULT_SPELLFILE_PATH);
  99.         spellBook = new Arcana.SpellBook();
  100.         lastSpell = new Arcana.SpellRecord();
  101.         currentSpell = new Arcana.SpellRecord();
  102.         }
  103.         
  104.     public void mainline() {
  105.         String selector;
  106.         
  107.         while(true) {
  108.             selector = hostFrame.getLine();
  109.             
  110.                  if (selector.compareTo("") == 0) {
  111.                     hostFrame.printEndLine("You didn't enter anything!");
  112.                     }
  113.             else if (selector.compareTo("test?") == 0) {
  114.                     hostFrame.printEndLine("SpellCompositor mainline");
  115.                     }
  116.             else if (selector.compareTo("Commands?") == 0 ||
  117.                      selector.compareTo("?") == 0) {
  118.                      hostFrame.printEndLine("?, about, "
  119.                                              + "createSpell, "
  120.                                              + "parseSpell, "
  121.                                              + "removeSpell, "
  122.                                              + "save, "
  123.                                              + "load, "
  124.                                              + "displaySpell, "
  125.                                              + "findSpell, "
  126.                                              + "viewBook, "
  127.                                              + "noOfSpells, "
  128.                                              + "showBookInfo, "
  129.                                              + "registerBook, "
  130.                                              + "Quit");
  131.                     }
  132.             else if (selector.compareTo("about") == 0) {
  133.                     about();
  134.                     }
  135.             else if (selector.compareTo("createSpell") == 0) {
  136.                     createSpell();
  137.                     }
  138.             else if (selector.compareTo("parseSpell") == 0) {
  139.                     parseSpell();
  140.                     }
  141.             else if (selector.compareTo("removeSpell") == 0) {
  142.                     removeSpell();
  143.                     }
  144.             else if (selector.compareTo("save") == 0) {
  145.                     save();
  146.                     }
  147.             else if (selector.compareTo("load") == 0) {
  148.                     load();
  149.                     }
  150.             else if (selector.compareTo("displaySpell") == 0) {
  151.                     displaySpell();
  152.                     }
  153.             else if (selector.compareTo("findSpell") == 0) {
  154.                     findSpellInBook();
  155.                     }
  156.             else if (selector.compareTo("viewBook") == 0) {
  157.                     showBrowser();
  158.                     //viewBook();
  159.                     }
  160.             else if (selector.compareTo("noOfSpells") == 0) {
  161.                     hostFrame.printEndLine("# of Spells: " + spellBook.getNoOfSpellsInBook());
  162.                     }
  163.             else if (selector.compareTo("showBookInfo") == 0) {
  164.                     hostFrame.printEndLine("Spellbook " + spellBook.getName() 
  165.                             + ", created by " + spellBook.getCreator() 
  166.                             + " and owned by " + spellBook.getOwner() + ".");
  167.                     }
  168.             else if (selector.compareTo("registerBook") == 0) {
  169.                     registerBook();
  170.                     }
  171.             else if (selector.compareTo("fuckTouch") == 0) {
  172.                     fileTouched = false;
  173.                     hostFrame.printEndLine("Touch is fucked.  Go ahead and load!");
  174.                     }
  175.             else if (selector.compareTo("Quit") == 0) {    
  176.                     hostFrame.printLine("Exiting Shell... Quiessence.");
  177.                     System.exit(0);
  178.                     break;
  179.                     }
  180.             else { // default case
  181.                 hostFrame.printEndLine("The command \""+ selector +"\" was not recognized!");
  182.                 }
  183.             }
  184.         }
  185.         
  186.     public void about() {
  187.         hostFrame.printLine("Spell Compositor - Arcana SpellBook");
  188.         hostFrame.printLine("written by Scott C. Ziegler, 2000-2001");
  189.         hostFrame.printLine("presented at MacHack 2001 on MacOS X");
  190.         hostFrame.printLine("     This small application was developed during experimentations with the Java programming language and its Java Foundation Classes, including Swing.  Meant as an \"in-house\" editing program, this app flourished into an ALMOST useful spellbook archiving program.  This app is dedicated to my friends from days of old when magick filled the aire, and to those who have ever felt the age-smoothed edges of a polyhedron as it decided the fate of some hapless character.");
  191.         hostFrame.printEndLine("You never saw this fnord.");
  192.         }
  193.         
  194.         
  195.     private void createSpell() {
  196.         showEditor();
  197.         editFrame.clearFields();
  198.         }
  199.         
  200.     private void parseSpell() {
  201.         buffer = new char[4096];
  202.         bufferIndex = 0;
  203.         bytesRead = 0;
  204.         Arcana.SpellRecord spellRec = new Arcana.SpellRecord();
  205.         String selector = new String("");
  206.         
  207.         while(true) {
  208.             hostFrame.printEndLine("Enter filename of text file to parse as a spell:");
  209.             selector = hostFrame.getLine();
  210.             
  211.             if (selector.compareTo("") == 0) {
  212.                 hostFrame.printEndLine("That is an invalid filename!");
  213.                 continue;
  214.                 }
  215.             else {
  216.                 try {
  217.                     fr = new FileReader(selector);
  218.                     in = new BufferedReader(fr);
  219.                         
  220.                     spellRec.setLevel(Integer.parseInt(in.readLine()));
  221.                     spellRec.setName(in.readLine());
  222.                     hostFrame.printLine("Name: " + spellRec.getName());
  223.                     spellRec.setSchool(in.readLine());
  224.                     //hostFrame.printLine("School: " + spellRec.getSchool());
  225.                     spellRec.setRange(in.readLine());
  226.                     spellRec.setComponentsByString(in.readLine());
  227.                     spellRec.setDuration(in.readLine());
  228.                     spellRec.setCastingTime(in.readLine());
  229.                     spellRec.setAreaOfEffect(in.readLine());
  230.                     spellRec.setSavingThrow(in.readLine());
  231.                     while((bytesRead = in.read()) != -1) {
  232.                         asciiChar = (short)bytesRead;
  233.                         buffer[bufferIndex] = (char)asciiChar;
  234.                         bufferIndex++;
  235.                         }
  236.                     spellRec.setEffect(new Arcana.SpellDescription(new String(buffer,0,bufferIndex)));
  237.                     
  238.                     hostFrame.printLine("File parsed...");
  239.                     in.close();
  240.                     lastSpell = spellRec;
  241.                     spellBook.addSpellRecord(spellRec);
  242.                     touch();
  243.                     if (spellBook.getSpellRecordVector(spellRec.getLevel()).contains(spellRec)) {
  244.                         hostFrame.printEndLine("Spell Added to Book...");
  245.                         }
  246.                     else hostFrame.printEndLine("Spell Not Added!  FUCK! A BUG!");
  247.                     break;
  248.                     }
  249.                 catch (FileNotFoundException fnfe) {
  250.                     hostFrame.printLine("Error -- " + fnfe.toString());
  251.                     hostFrame.printEndLine("Cannot Find the file you requested");
  252.                     continue;
  253.                     }
  254.                 catch (IOException ie) {
  255.                     hostFrame.printEndLine("Error -- " + ie.toString());
  256.                     break;
  257.                     }
  258.                 catch (NumberFormatException nfe) {
  259.                     hostFrame.printEndLine("Error -- " + nfe.toString());
  260.                     //continue;
  261.                     break;
  262.                     }
  263.                 }
  264.             }
  265.         }
  266.         
  267.     private void removeSpell() {
  268.         // Need to add an are you sure dialog...
  269.         spellBook.removeSpellRecord(currentSpell);
  270.         // need to check for if the removal is the last spell in the level
  271.         // need to evaluate this in general... what happens when you delete the last spell?
  272.         if (spellBook.getSpellRecordVector(currentLevel).isEmpty()) currentSpell = new Arcana.SpellRecord();
  273.         else if (!(currentIndexInLevel <= 0)) {
  274.             currentSpell = spellBook.getSpellRegisterVector(currentLevel)
  275.                                     .getSpellRegister(currentIndexInLevel-1)
  276.                                     .getSourceSpell();
  277.             }
  278.         else currentSpell = spellBook.getSpellRegisterVector(currentLevel)
  279.                                     .getSpellRegister(currentIndexInLevel+1)
  280.                                     .getSourceSpell();
  281.         }
  282.         
  283.     private void save() {
  284.         if (fileTouched) {
  285.             spellFile.save(spellBook);
  286.             hostFrame.printLine("Saved");
  287.             hostFrame.printEndLine("No. of Spells in Book:" + spellBook.getNoOfSpellsInBook());
  288.             }
  289.         fileTouched = false;
  290.         
  291.         }
  292.         
  293.     private void load() {
  294.         if (!fileTouched) {
  295.             spellBook = spellFile.load();
  296.             currentSpell = spellBook.getSpellRegisterVector(currentLevel)
  297.                                     .getSpellRegister(currentIndexInLevel)
  298.                                     .getSourceSpell();
  299.             hostFrame.printLine("Loaded.");
  300.             hostFrame.printEndLine("No. of Spells in Book:" + spellBook.getNoOfSpellsInBook());
  301.             }
  302.         else hostFrame.printEndLine("Cannot load till changes are saved!");
  303.         }
  304.         
  305.     private void displaySpell() {
  306.         if (lastSpell != null && currentSpell != null) {
  307.             /*
  308.             hostFrame.printLine("Spell Record: ");
  309.             hostFrame.printLine("Level: " + lastSpell.getLevel());
  310.             hostFrame.printLine("Name: " + lastSpell.getName());
  311.             hostFrame.printLine("School: " + lastSpell.getSchool());
  312.             hostFrame.printLine("Range: " + lastSpell.getRange());
  313.             hostFrame.printLine("Components: " + lastSpell.getComponentsString());
  314.             hostFrame.printLine("Duration: " + lastSpell.getDuration());
  315.             hostFrame.printLine("Casting Time: " + lastSpell.getCastingTime());
  316.             hostFrame.printLine("AreaOfEffect: " + lastSpell.getAreaOfEffect());
  317.             hostFrame.printLine("SavingThrow: " + lastSpell.getSavingThrow());
  318.             hostFrame.printEndLine("Description: " + lastSpell.getEffect().getDescription());
  319.             */
  320.             
  321.             viewFrame.displaySpell(currentSpell);
  322.             showViewer();
  323.             }
  324.         else {
  325.             hostFrame.printEndLine("There isn't a spell yet!");
  326.             }
  327.         }
  328.         
  329.     private void findSpellInBook() {
  330.         String name = new String("");
  331.         String levelStr = new String("");
  332.         int level;
  333.         
  334.         while (true) {
  335.             hostFrame.printEndLine("Enter the name of the spell (exactly).");
  336.             name = hostFrame.getLine();
  337.             hostFrame.printEndLine("Enter the level of the spell or \"!\" if unknown.");
  338.             levelStr = hostFrame.getLine();
  339.             if (levelStr.compareTo("!") == 0) {
  340.                 currentSpell = spellBook.findSpellByName(name);
  341.                 showViewer();
  342.                 break;
  343.                 }
  344.             try {
  345.                 Integer i = new Integer(levelStr);
  346.                 level = i.intValue();
  347.                 if (level > 0 && level <= 9) {
  348.                     currentSpell = spellBook.findSpell(name, level);
  349.                     showViewer();
  350.                     }
  351.                 break;
  352.                 }
  353.             catch (NumberFormatException nfx) {
  354.                 hostFrame.printEndLine("Shit Happens when you enter invalid ints.");
  355.                 continue;
  356.                 }
  357.             }
  358.         }
  359.         
  360.     private void viewBook() {
  361.         String selector;
  362.         Arcana.SpellRecord currentSpell = new Arcana.SpellRecord();
  363.         int currentLevel = 1;
  364.         int currentIndexInLevel = 0;
  365.         
  366.         try {
  367.             hostFrame.printLine("Viewing SpellBook... (help is \"?\")");
  368.             currentSpell = spellBook.getSpellRegisterVector(currentLevel)
  369.                                     .getSpellRegister(0)
  370.                                     .getSourceSpell();
  371.             //System.out.println("current spell initialized");
  372.             while(true) {
  373.                 selector = hostFrame.getLine();
  374.                 
  375.                      if (selector.compareTo("") == 0) {
  376.                         hostFrame.printEndLine("You didn't enter anything!");
  377.                         }
  378.                 else if (selector.compareTo("test?") == 0) {
  379.                         hostFrame.printEndLine("ViewBook");
  380.                         }
  381.                 else if (selector.compareTo("Commands?") == 0 ||
  382.                          selector.compareTo("?") == 0) {
  383.                          hostFrame.printEndLine("?, displaySpell, "
  384.                                                  + "nextLevel, "
  385.                                                  + "prevLevel, "
  386.                                                  + "levelList, "
  387.                                                  + "nextSpell, "
  388.                                                  + "prevSpell, "
  389.                                                  + "End");
  390.                         }
  391.                 else if (selector.compareTo("displaySpell") == 0) {
  392.                         if (currentSpell != null) {
  393.                             hostFrame.printLine("Spell Record: ");
  394.                             hostFrame.printLine("Level: " + currentSpell.getLevel());
  395.                             hostFrame.printLine("Name: " + currentSpell.getName());
  396.                             hostFrame.printLine("School: " + currentSpell.getSchool());
  397.                             hostFrame.printLine("Range: " + currentSpell.getRange());
  398.                             hostFrame.printLine("Components: " + currentSpell.getComponentsString());
  399.                             hostFrame.printLine("Duration: " + currentSpell.getDuration());
  400.                             hostFrame.printLine("Casting Time: " + currentSpell.getCastingTime());
  401.                             hostFrame.printLine("AreaOfEffect: " + currentSpell.getAreaOfEffect());
  402.                             hostFrame.printLine("SavingThrow: " + currentSpell.getSavingThrow());
  403.                             hostFrame.printEndLine("Description: " + currentSpell.getEffect().getDescription());
  404.                             }
  405.                         else {
  406.                             hostFrame.printEndLine("There is no spell currently.");
  407.                             }
  408.                         }
  409.                 else if (selector.compareTo("nextLevel") == 0) {
  410.                         if (currentLevel + 1 < 9) {
  411.                             ++currentLevel;
  412.                             currentIndexInLevel = 0;
  413.                             currentSpell = spellBook.getSpellRegisterVector(currentLevel)
  414.                                                     .getSpellRegister(currentIndexInLevel)
  415.                                                     .getSourceSpell();
  416.                             hostFrame.printEndLine("Now On Level: " + currentLevel);
  417.                             }
  418.                         else hostFrame.printEndLine("Maximum Level!");
  419.                         }
  420.                 else if (selector.compareTo("prevLevel") == 0) {
  421.                         if (currentLevel - 1 < 1) {
  422.                             --currentLevel;
  423.                             currentIndexInLevel = 0;
  424.                             currentSpell = spellBook.getSpellRegisterVector(currentLevel)
  425.                                                     .getSpellRegister(currentIndexInLevel)
  426.                                                     .getSourceSpell();
  427.                             hostFrame.printEndLine("Now On Level: " + currentLevel);
  428.                             }
  429.                         else hostFrame.printEndLine("Minimum Level!");
  430.                         }
  431.                 else if (selector.compareTo("levelList") == 0) {
  432.                         Vector vec = spellBook.getSpellRegisterVector(currentLevel)
  433.                                               .getSpellNamesVector();
  434.                         Enumeration e = vec.elements();
  435.                         while (e.hasMoreElements()) {
  436.                             hostFrame.printLine((String)e.nextElement());
  437.                             }
  438.                         hostFrame.printEndLine("<End>");
  439.                         }
  440.                 else if (selector.compareTo("nextSpell") == 0) {
  441.                         if (!(currentIndexInLevel > 
  442.                                 spellBook.getSpellRegisterVector(currentLevel).getSpellRegisterVector().size())) {
  443.                             ++currentIndexInLevel;
  444.                             currentSpell = spellBook.getSpellRegisterVector(currentLevel)
  445.                                                     .getSpellRegister(currentIndexInLevel)
  446.                                                     .getSourceSpell();
  447.                             hostFrame.printEndLine("Currently At: " + currentSpell.getName());
  448.                             }
  449.                         else {
  450.                             hostFrame.printEndLine("No More Spells!");
  451.                             }
  452.                         }
  453.                 else if (selector.compareTo("prevSpell") == 0) {
  454.                         if (!(currentIndexInLevel < 0)) {
  455.                             --currentIndexInLevel;
  456.                             currentSpell = spellBook.getSpellRegisterVector(currentLevel)
  457.                                                     .getSpellRegister(currentIndexInLevel)
  458.                                                     .getSourceSpell();
  459.                             hostFrame.printEndLine("Currently At: " + currentSpell.getName());
  460.                             }
  461.                         else {
  462.                             hostFrame.printEndLine("Already at first spell");
  463.                             }
  464.                         }
  465.                 else if (selector.compareTo("End") == 0) {
  466.                         hostFrame.printEndLine("Ending ViewBook Routine.");
  467.                         break;
  468.                         }
  469.                 }
  470.             }
  471.         catch(NoSuchElementException nsee) {
  472.             hostFrame.printEndLine("That spell does not exist!");
  473.             }
  474.         }
  475.         
  476.     private void registerBook() {
  477.         String name = new String("");
  478.         String creator = new String("");
  479.         String owner = new String("");
  480.         
  481.         while(true) {
  482.             hostFrame.printEndLine("Enter the Name of the Spellbook:");
  483.             name = hostFrame.getLine();
  484.             
  485.             if (name.compareTo("") == 0) {
  486.                 hostFrame.printEndLine("That is an invalid Name!");
  487.                 continue;
  488.                 }
  489.             else {
  490.                 spellBook.setName(name);
  491.                 break;
  492.                 }
  493.             }
  494.         while(true) {
  495.             hostFrame.printEndLine("Enter the Creator of the Spellbook:");
  496.             creator = hostFrame.getLine();
  497.             
  498.             if (name.compareTo("") == 0) {
  499.                 hostFrame.printEndLine("That is an invalid Creator!");
  500.                 continue;
  501.                 }
  502.             else {
  503.                 spellBook.setCreator(creator);
  504.                 break;
  505.                 }
  506.             }
  507.         while(true) {
  508.             hostFrame.printEndLine("Enter the Owner of the Spellbook:");
  509.             owner = hostFrame.getLine();
  510.             
  511.             if (name.compareTo("") == 0) {
  512.                 hostFrame.printEndLine("That is an invalid Owner!");
  513.                 continue;
  514.                 }
  515.             else {
  516.                 spellBook.setOwner(owner);
  517.                 break;
  518.                 }
  519.             }
  520.         hostFrame.printEndLine("Spellbook " + spellBook.getName() 
  521.                             + ", created by " + spellBook.getCreator() 
  522.                             + " and owned by " + spellBook.getOwner() + ".");
  523.         touch();
  524.         }
  525.         
  526.     private void initEditor() {
  527.         editFrame.setNavigationListener(new ActionListener() {
  528.             public void actionPerformed(ActionEvent ae) {
  529.                 String com = ae.getActionCommand();
  530.                      if (com.compareTo("Back") == 0) {
  531.                          backSpell();
  532.                         }
  533.                 else if (com.compareTo("Next") == 0) {
  534.                     nextSpell();
  535.                     }
  536.                 else if (com.compareTo("PrevLvl") == 0) {
  537.                     prevLvl();
  538.                     }
  539.                 else if (com.compareTo("FwdLvl") == 0) {
  540.                     fwdLvl();
  541.                     }
  542.                 }
  543.             });
  544.         editFrame.setManipulationListener(new ActionListener() {
  545.             public void actionPerformed(ActionEvent ae) {
  546.                 String com = ae.getActionCommand();
  547.                      if (com.compareTo("View") == 0) {
  548.                          showViewer();
  549.                          viewFrame.displaySpell(currentSpell);
  550.                         }
  551.                 else if (com.compareTo("Save") == 0) {
  552.                     Arcana.SpellRecord spellRec = new Arcana.SpellRecord();
  553.                     spellRec = editFrame.getSpell();
  554.                     // need to check for empty record (it should not be added)
  555.                     spellBook.addSpellRecord(spellRec);
  556.                     touch();
  557.                     lastSpell = spellRec;
  558.                     currentSpell = spellRec;
  559.                     
  560.                     // !!! Check for duplicate spells by checking spell name (no two spells can have same name)
  561.                     // the above is now taken care of in SpellBook addSpellRecord
  562.                     
  563.                     if (spellBook.getSpellRecordVector(spellRec.getLevel()).contains(spellRec)) {
  564.                         hostFrame.printEndLine("Spell Added to Book...");
  565.                         }
  566.                     else hostFrame.printEndLine("Spell Not Added!  FUCK! A BUG!");
  567.                     
  568.                     //editFrame.setVisible(false);
  569.                     editFrame.clearFields();
  570.                     viewFrame.displaySpell(currentSpell);
  571.                     showViewer();
  572.                     }
  573.                 else if (com.compareTo("Clear") == 0) {
  574.                     editFrame.clearFields();
  575.                     }
  576.                 else if (com.compareTo("New") == 0) {
  577.                     editFrame.clearFields();
  578.                     }
  579.                 }
  580.             });
  581.         }
  582.         
  583.     private void initViewer() {
  584.         viewFrame.setNavigationListener(new ActionListener() {
  585.             public void actionPerformed(ActionEvent ae) {
  586.                 String com = ae.getActionCommand();
  587.                      if (com.compareTo("Back") == 0) {
  588.                         backSpell();
  589.                         }
  590.                 else if (com.compareTo("Next") == 0) {
  591.                     nextSpell();
  592.                     }
  593.                 else if (com.compareTo("PrevLvl") == 0) {
  594.                     prevLvl();
  595.                     }
  596.                     
  597.                 else if (com.compareTo("FwdLvl") == 0) {
  598.                     fwdLvl();
  599.                     }
  600.                 }
  601.             });
  602.         viewFrame.setManipulationListener(new ActionListener() {
  603.             public void actionPerformed(ActionEvent ae) {
  604.                 String com = ae.getActionCommand();
  605.                      if (com.compareTo("Edit") == 0) {
  606.                          editSpell();
  607.                         }
  608.                 else if (com.compareTo("Delete") == 0) {
  609.                     // need to implement after delete function is implemented
  610.                     deleteSpell();
  611.                     }
  612.                 else if (com.compareTo("Find") == 0) {
  613.                     // need to implement w/ JOptionPane
  614.                     find();
  615.                     }
  616.                 else if (com.compareTo("New") == 0) {
  617.                     newSpell();
  618.                     }
  619.                 }
  620.             });
  621.         }
  622.         
  623.     private void initBrowser() {
  624.         browserFrame.setBackListener(new ActionListener() {
  625.             public void actionPerformed(ActionEvent ae) {
  626.                 if (browserFrame.getLevelList().hasFocus()) {
  627.                     prevLvl();
  628.                     }
  629.                 if (browserFrame.getSpellsList().hasFocus()) {
  630.                     backSpell();
  631.                     }
  632.                 }
  633.             });
  634.         browserFrame.setFwdListener(new ActionListener() {
  635.             public void actionPerformed(ActionEvent ae) {
  636.                 if (browserFrame.getLevelList().hasFocus()) {
  637.                     fwdLvl();
  638.                     }
  639.                 if (browserFrame.getSpellsList().hasFocus()) {
  640.                     nextSpell();
  641.                     }
  642.                 }
  643.             });
  644.         browserFrame.setViewListener(new ActionListener() {
  645.             public void actionPerformed(ActionEvent ae) {
  646.                 viewSpell();
  647.                 }
  648.             });
  649.         browserFrame.setEditListener(new ActionListener() {
  650.             public void actionPerformed(ActionEvent ae) {
  651.                 editSpell();
  652.                 }
  653.             });
  654.         browserFrame.setDeleteListener(new ActionListener() {
  655.             public void actionPerformed(ActionEvent ae) {
  656.                 deleteSpell();
  657.                 }
  658.             });
  659.         browserFrame.setLoadListener(new ActionListener() {
  660.             public void actionPerformed(ActionEvent ae) {
  661.                 //deleteSpell();
  662.                 }
  663.             });
  664.         browserFrame.setSaveListener(new ActionListener() {
  665.             public void actionPerformed(ActionEvent ae) {
  666.                 //deleteSpell();
  667.                 }
  668.             });
  669.         browserFrame.setSaveAsListener(new ActionListener() {
  670.             public void actionPerformed(ActionEvent ae) {
  671.                 //deleteSpell();
  672.                 }
  673.             });
  674.         browserFrame.setLevelListSelectionListener(new ListSelectionListener() {
  675.             public void valueChanged(ListSelectionEvent lse) {
  676.                 currentLevel = ((JList)lse.getSource()).getSelectedIndex() + 1;
  677.                 browserFrame.changeSpellsModel(spellBook.getSpellRegisterVector(currentLevel)
  678.                                                        .getSpellNamesVector());
  679.                 }
  680.             });
  681.         browserFrame.setSpellsListSelectionListener(new ListSelectionListener() {
  682.             public void valueChanged(ListSelectionEvent lse) {
  683.                 currentIndexInLevel = ((JList)lse.getSource()).getSelectedIndex();
  684.                 if (currentIndexInLevel < 0) { currentIndexInLevel = 0; }
  685.                 currentSpell = spellBook.getSpellRegisterVector(currentLevel)
  686.                                         .getSpellRegister(currentIndexInLevel)
  687.                                         .getSourceSpell();
  688.                 }
  689.             });
  690.         browserFrame.setSpellsListMouseListener(new MouseAdapter() {
  691.             public void mouseClicked(MouseEvent me) {
  692.                 if (me.getID() == MouseEvent.MOUSE_CLICKED && me.getClickCount() >= 2) {
  693.                     viewSpell();
  694.                     }
  695.                 }
  696.             });
  697.         }
  698.         
  699.     private void showEditor() {
  700.         viewFrame.setVisible(false);
  701.         editFrame.displaySpell(currentSpell);
  702.         editFrame.setVisible(true);
  703.         }
  704.         
  705.     private void showViewer() {
  706.         editFrame.setVisible(false);
  707.         viewFrame.displaySpell(currentSpell);
  708.         viewFrame.setVisible(true);
  709.         }
  710.         
  711.     private void showBrowser() {
  712.         browserFrame.changeSpellsModel(spellBook.getSpellRegisterVector(currentLevel)
  713.                                                 .getSpellNamesVector());
  714.         browserFrame.setVisible(true);
  715.         }
  716.         
  717.     private void touch() {
  718.         browserFrame.undimSave();
  719.         fileTouched = true; 
  720.         }
  721.     
  722.     private void menuSave() {
  723.         if (fileTouched) {
  724.             spellFile.save(spellBook);
  725.             browserFrame.dimSave();
  726.             fileTouched = false;
  727.             }
  728.         }
  729.         
  730.     private void menuLoad() {
  731.         if (fileTouched) {
  732.             int choice = JOptionPane.showConfirmDialog(browserFrame, "This book is not saved! Load anyways? (i.e. data loss)", 
  733.                             "Book Not Saved!", JOptionPane.OK_CANCEL_OPTION, 
  734.                             JOptionPane.WARNING_MESSAGE);
  735.             switch (choice) {
  736.                 case JOptionPane.OK_OPTION:
  737.                     spellBook = spellFile.loadFrom((Component)browserFrame);  // what the fuck is this?  trying something else...
  738.                     //spellBook = spellFile.load();
  739.                     currentSpell = spellBook.getSpellRegisterVector(currentLevel)
  740.                                             .getSpellRegister(currentIndexInLevel)
  741.                                             .getSourceSpell();
  742.                     break;
  743.                 case JOptionPane.CANCEL_OPTION:
  744.                 case JOptionPane.CLOSED_OPTION:
  745.                     break;
  746.                 }
  747.             }
  748.         }
  749.         
  750.     private void menuSaveAs() {
  751.         spellFile.saveAs(spellBook, (Component)browserFrame);
  752.         }
  753.     
  754.     private void prevLvl() {
  755.         if (currentLevel - 1 >= 1) {
  756.             --currentLevel;
  757.             currentIndexInLevel = 0;
  758.             currentSpell = spellBook.getSpellRegisterVector(currentLevel)
  759.                                     .getSpellRegister(currentIndexInLevel)
  760.                                     .getSourceSpell();
  761.             browserFrame.changeSpellsModel(spellBook.getSpellRegisterVector(currentLevel)
  762.                                                     .getSpellNamesVector());
  763.             browserFrame.getSpellsList().setSelectedIndex(currentIndexInLevel);
  764.             browserFrame.getLevelList().setSelectedIndex(currentLevel - 1); // to be an index
  765.             viewFrame.displaySpell(currentSpell);
  766.             editFrame.displaySpell(currentSpell);
  767.             }
  768.         }
  769.         
  770.     private void fwdLvl() {
  771.         if (currentLevel + 1 <= 9) {
  772.             ++currentLevel;
  773.             currentIndexInLevel = 0;
  774.             currentSpell = spellBook.getSpellRegisterVector(currentLevel)
  775.                                     .getSpellRegister(currentIndexInLevel)
  776.                                     .getSourceSpell();
  777.             browserFrame.changeSpellsModel(spellBook.getSpellRegisterVector(currentLevel)
  778.                                                     .getSpellNamesVector());
  779.             browserFrame.getSpellsList().setSelectedIndex(currentIndexInLevel);
  780.             browserFrame.getLevelList().setSelectedIndex(currentLevel - 1); // to be an index
  781.             viewFrame.displaySpell(currentSpell);
  782.             editFrame.displaySpell(currentSpell);
  783.             }
  784.         }
  785.         
  786.     private void nextSpell() {
  787.         if (!(currentIndexInLevel >= spellBook.getSpellRegisterVector(currentLevel).getSpellRegisterVector().size() - 1)) {
  788.             ++currentIndexInLevel;
  789.             currentSpell = spellBook.getSpellRegisterVector(currentLevel)
  790.                                                     .getSpellRegister(currentIndexInLevel)
  791.                                                     .getSourceSpell();
  792.             browserFrame.getSpellsList().setSelectedIndex(currentIndexInLevel);
  793.             viewFrame.displaySpell(currentSpell);
  794.             editFrame.displaySpell(currentSpell);
  795.             }
  796.         }
  797.     
  798.     private void backSpell() {
  799.         if (!(currentIndexInLevel <= 0)) {
  800.             --currentIndexInLevel;
  801.             currentSpell = spellBook.getSpellRegisterVector(currentLevel)
  802.                                                     .getSpellRegister(currentIndexInLevel)
  803.                                                     .getSourceSpell();
  804.             browserFrame.getSpellsList().setSelectedIndex(currentIndexInLevel);
  805.             viewFrame.displaySpell(currentSpell);
  806.             editFrame.displaySpell(currentSpell);
  807.             }
  808.         }
  809.         
  810.     private void newSpell() {
  811.         showEditor();
  812.         editFrame.clearFields();
  813.         }
  814.         
  815.     private void editSpell() { showEditor(); }
  816.         
  817.     private void viewSpell() { showViewer(); }
  818.         
  819.     private void deleteSpell() {
  820.         removeSpell();
  821.         viewFrame.displaySpell(currentSpell);
  822.         }
  823.     
  824.     private void find() {
  825.         String searchStr = JOptionPane.showInputDialog(browserFrame, "Enter the spell's name:", "Find…", JOptionPane.QUESTION_MESSAGE);
  826.         if (searchStr != null) {
  827.             currentSpell = spellBook.findSpellByName(searchStr);
  828.             if (currentSpell.getName().compareTo("Untitled") == 0) {
  829.                 JOptionPane.showMessageDialog(browserFrame, "That spell was not found!", 
  830.                                         "Error: Spell Not Found", JOptionPane.ERROR_MESSAGE);
  831.                 }
  832.             else showViewer();
  833.             }
  834.         }
  835.  
  836.     // Main entry point
  837.     static public void main(String[] args) {
  838.         SpellCompositor spellCompApp = new SpellCompositor();
  839.         spellCompApp.mainline();
  840.         }
  841.     
  842. }
  843.